package com.crk.yiyuanguahao.service.impl;

import com.crk.yiyuanguahao.dao.DoctorAppointmentDao;
import com.crk.yiyuanguahao.dao.DoctorCommentDao;
import com.crk.yiyuanguahao.dao.DoctorDao;
import com.crk.yiyuanguahao.entity.DoctorComment;
import com.crk.yiyuanguahao.entity.DoctorCommentDto;
import com.crk.yiyuanguahao.entity.User;
import com.crk.yiyuanguahao.service.DoctorCommentService;
import com.crk.yiyuanguahao.util.Page;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @description:
 * @date 2020/3/26
 */
@Service
public class DoctorCommentServiceImpl implements DoctorCommentService {

    @Autowired
    DoctorCommentDao doctorCommentDao;
    @Autowired
    DoctorDao doctorDao;
    @Autowired
    DoctorAppointmentDao doctorAppointmentDao;

    /**
     * 保存评价信息
     *
     * @param doctorComment 评价
     * @return
     */
    @Override
    public ResponseResult saveDoctorComment(DoctorCommentDto doctorComment) {
        if (null != doctorComment){
            int result = 0;
            if (doctorComment.getId() == null){
                doctorComment.setCreateTime(new Date());
                User user = (User) SecurityUtils.getSubject().getPrincipal();
                doctorComment.setCreator(user.getId());
                result = doctorCommentDao.insert(doctorComment);
                if (doctorComment.isUserComment()){
                    doctorAppointmentDao.updateStatus(doctorComment.getAppointmentId(),2);
                }
            }else{
                User user = (User) SecurityUtils.getSubject().getPrincipal();
                doctorComment.setCreator(user.getId());
                result = doctorCommentDao.updateByPrimaryKey(doctorComment);
            }

            if (result > 0){
                return ResponseResult.success(doctorComment);
            }else{
                return ResponseResult.fail();
            }
        }else {
            return ResponseResult.fail();
        }
    }

    /**
     * 删除评价信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteDoctorComment(Integer id) {
        int i = doctorCommentDao.deleteByPrimaryKey(id);
        if (i>0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    /**
     * 查询评价信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getDoctorComment(Integer id) {
        DoctorComment doctorComment = doctorCommentDao.selectByPrimaryKey(id);
        DoctorCommentDto doctorCommentDto = new DoctorCommentDto();
        doctorCommentDto.setDoctorName(doctorDao.selectDoctorByCode(doctorComment.getDoctorCode()).getDoctorName());
        doctorCommentDto.setId(doctorComment.getId());
        doctorCommentDto.setAppointmentId(doctorComment.getAppointmentId());
        doctorCommentDto.setScore(doctorComment.getScore());
        doctorCommentDto.setContentType(doctorComment.getContentType());
        doctorCommentDto.setContent(doctorComment.getContent());
        doctorCommentDto.setCreateTime(doctorComment.getCreateTime());
        doctorCommentDto.setDoctorCode(doctorComment.getDoctorCode());
        return ResponseResult.success(doctorCommentDto);
    }

    /**
     * 查询评价信息列表
     *
     * @param doctorCode 医生编号
     * @param pageInfo   分页
     * @return
     */
    @Override
    public ResponseResult queryDoctorCommentPage(String doctorCode, Page pageInfo) {
        Integer patientId = null;
        Integer count = doctorCommentDao.countByCondition(doctorCode,patientId);
        pageInfo.setAllRow(count);
        List<DoctorComment> doctorList = doctorCommentDao.selectByCondition(doctorCode,patientId, pageInfo.getStart(), pageInfo.getSize());
        List<DoctorCommentDto> doctorCommentDtos = new ArrayList<>();
        for (DoctorComment doctorComment : doctorList) {
            DoctorCommentDto doctorCommentDto = new DoctorCommentDto();
            doctorCommentDto.setDoctorName(doctorDao.selectDoctorByCode(doctorComment.getDoctorCode()).getDoctorName());
            doctorCommentDto.setId(doctorComment.getId());
            doctorCommentDto.setAppointmentId(doctorComment.getAppointmentId());
            doctorCommentDto.setScore(doctorComment.getScore());
            doctorCommentDto.setContentType(doctorComment.getContentType());
            doctorCommentDto.setContent(doctorComment.getContent());
            doctorCommentDto.setDoctorCode(doctorComment.getDoctorCode());
            doctorCommentDto.setCreateTime(doctorComment.getCreateTime());
            doctorCommentDtos.add(doctorCommentDto);
        }
        return ResponseResult.success(doctorCommentDtos);
    }

    @Override
    public ResponseResult queryMyDoctorCommentPage(String doctorCode, Page pageInfo) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int patientId = user.getId();
        Integer count = doctorCommentDao.countByCondition(doctorCode,patientId);
        pageInfo.setAllRow(count);
        List<DoctorComment> doctorList = doctorCommentDao.selectByCondition(doctorCode,patientId, pageInfo.getStart(), pageInfo.getSize());
        List<DoctorCommentDto> doctorCommentDtos = new ArrayList<>();
        for (DoctorComment doctorComment : doctorList) {
            DoctorCommentDto doctorCommentDto = new DoctorCommentDto();
            doctorCommentDto.setDoctorName(doctorDao.selectDoctorByCode(doctorComment.getDoctorCode()).getDoctorName());
            doctorCommentDto.setId(doctorComment.getId());
            doctorCommentDto.setAppointmentId(doctorComment.getAppointmentId());
            doctorCommentDto.setScore(doctorComment.getScore());
            doctorCommentDto.setContentType(doctorComment.getContentType());
            doctorCommentDto.setContent(doctorComment.getContent());
            doctorCommentDto.setDoctorCode(doctorComment.getDoctorCode());
            doctorCommentDto.setCreateTime(doctorComment.getCreateTime());
            doctorCommentDtos.add(doctorCommentDto);
        }
        return ResponseResult.success(doctorCommentDtos);
    }

    @Override
    public ResponseResult querySelfDoctorCommentPage(Page pageInfo) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String doctorCode = user.getDoctorCode();
        Integer count = doctorCommentDao.countByCondition(doctorCode,null);
        pageInfo.setAllRow(count);
        List<DoctorComment> doctorList = doctorCommentDao.selectByCondition(doctorCode,null, pageInfo.getStart(), pageInfo.getSize());
        List<DoctorCommentDto> doctorCommentDtos = new ArrayList<>();
        for (DoctorComment doctorComment : doctorList) {
            DoctorCommentDto doctorCommentDto = new DoctorCommentDto();
            doctorCommentDto.setDoctorName(doctorDao.selectDoctorByCode(doctorComment.getDoctorCode()).getDoctorName());
            doctorCommentDto.setId(doctorComment.getId());
            doctorCommentDto.setAppointmentId(doctorComment.getAppointmentId());
            doctorCommentDto.setScore(doctorComment.getScore());
            doctorCommentDto.setContentType(doctorComment.getContentType());
            doctorCommentDto.setContent(doctorComment.getContent());
            doctorCommentDto.setDoctorCode(doctorComment.getDoctorCode());
            doctorCommentDto.setCreateTime(doctorComment.getCreateTime());
            doctorCommentDtos.add(doctorCommentDto);
        }
        return ResponseResult.success(doctorCommentDtos);
    }

    @Override
    public ResponseResult getDoctorCommentByAppointment(Integer appointmentId) {
        DoctorComment doctorComment = doctorCommentDao.getDoctorCommentByAppointment(appointmentId);
        return ResponseResult.success(doctorComment);
    }
}
